/******************************************************************************
 * @file   :  item12.cpp
 * @brief  :  Having realistic expectations about the thread safely of STL containers
 * Created :  09/15/2010 12:06:45 AM
 * Revision:  none
 * Compiler:  gcc -Wall -Wextra item12.cpp
 *
 * @author :  coanor, coanor@gmail.com
 ******************************************************************************/
#include <vector>
#include <algorithm>

using namespace std;

template<typename container>
class lock {
public:
	lock(const container cont) : c(cont) { get_mutex_for(c); }
	~lock() { release_lock_for(c); }

	get_mutex_for(container) { can_use = false; }
	release_lock_for(container) { can_use = true; }
private:
	const container& c;
	bool can_use;
};

int main()
{
	vector<int> v;
	/* Create a new block */
	{
		lock<vector<int> > lock_for(v);
		vector<int>::iterator first5(find(v.begin(), v.end(), 5));
		if (first5 != v.end()) {
			*first5 = 0;
		}
	} /* close block automatically, releasing the mutex */
}
